package trabajoISW2.data;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import trabajoISW2.domain.implementations.CreditCard;

public class CreditCardDAO extends BaseDAO implements ICreditCardDAO {


	public CreditCardDAO(){
		
	}
	@Override
	public List<CreditCard> getCreditCards() {

		Query q = getEm().createQuery("Select c FROM CreditCard c");
		return q.getResultList();

	}

	@Override
	public CreditCard getCreditCard(String number) {

		return getEm().find(CreditCard.class, number);

	}

	@Override
	public CreditCard createCreditCard(CreditCard c) {
		getEm().getTransaction().begin();
		getEm().persist(c);
		getEm().getTransaction().commit();
		getEm().close();
		return c;
	}

	@Override
	public void updateCreditCard(CreditCard c) {
		getEm().getTransaction().begin();
		getEm().merge(c);
		getEm().getTransaction().commit();
		getEm().close();

	}

	@Override
	public void deleteCreditCard(String number) {

		CreditCard card = getEm().find(CreditCard.class, number);
		if (card != null){
			getEm().getTransaction().begin();
			getEm().remove(card);
			getEm().getTransaction().commit();
			getEm().close();
		}

	}
}
